


// Author:					HungNM
// Created:					29-07-2012
// Last Modified:			29-07-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class Configs
	{

		#region Constructors

	    public Configs()
		{}
	    
	
	    public Configs(
			long id) 
		{
	        GetConfigs(
				id); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private string keyword = string.Empty;
		private string data = string.Empty;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public string Keyword 
		{
			get { return keyword; }
			set { keyword = value; }
		}
		public string Data 
		{
			get { return data; }
			set { data = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of Configs.
		/// </summary>
		/// <param name="id"> id </param>
		private void GetConfigs(
			long id) 
		{
			try
			{
				using(IDataReader reader = DBConfigs.GetOne(
					id)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.keyword = reader["Keyword"].ToString();
					this.data = reader["Data"].ToString();
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of Configs. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBConfigs.Create(
					this.keyword, 
					this.data, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.id = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of Configs. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBConfigs.Update(
					this.id, 
					this.keyword, 
					this.data, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of Configs. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of Configs. Returns true on success.
		/// </summary>
		/// <param name="id"> id </param>
		/// <returns>bool</returns>
		public static bool Delete(
			long  id) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBConfigs.Delete(
					id); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of Configs. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBConfigs.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<Configs> LoadListFromReader(IDataReader reader)
		{
			List<Configs> configsList = new List<Configs>();
			try
			{
				while (reader.Read())
				{
					Configs configs = new Configs();
					configs.id = Convert.ToInt64(reader["Id"]);
					configs.keyword = reader["Keyword"].ToString();
					configs.data = reader["Data"].ToString();
					configs.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					configs.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					configsList.Add(configs);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return configsList;
		}
	
		/// <summary>
		/// Gets an IList with all instances of Configs.
		/// </summary>
		public static List<Configs> GetAll()
		{
			List<Configs> listConfigs = new List<Configs>();
			try
			{
				IDataReader reader = DBConfigs.GetAll();
				listConfigs = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listConfigs;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of Configs.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<Configs> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<Configs> listConfigs = new List<Configs>();
			try
			{
				IDataReader reader = DBConfigs.GetPage(pageNumber, pageSize, out totalPages);	
				listConfigs = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listConfigs;
		}
		
		#endregion

	}
}





